//成功或失败总提醒
let told = document.getElementById('told');
let toTold = document.getElementById('toTold');

function close() {
    told.style.display = 'none';
};

function open() {
    told.style.display = 'block';
};
document.getElementById('toldClose').addEventListener('click', function() {
        told.style.display = 'none';
    })
    //右边盒子内容显示
document.getElementById('Uimg').src = 'http://www.rushmc.top/' + localStorage.getItem('url');
document.getElementById('Uname').innerHTML = localStorage.getItem('username');
//左边盒子内容显示
function word() {
    let word = document.getElementById('word');
    let date = new Date();
    let h = date.getHours();
    if (h < 12) {
        word.innerHTML = localStorage.getItem('username') + ' 上午好呀，今天也要努力奋斗噢';
    } else if (h >= 12 && h < 18) {
        word.innerHTML = localStorage.getItem('username') + ' 下午好哇，快去吃点小点心吧hh';
    } else {
        word.innerHTML = localStorage.getItem('username') + ' 晚上好啊，今天注意别熬夜了喔~';
    }
}
word();
//猜数字游戏 
function getRandom(max, min) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
document.getElementById('go').addEventListener('click', function() {
        document.getElementById('yes').addEventListener('click', function() {
            var random = getRandom(1, 50);
            document.getElementById('remind').innerHTML = ' ';
            let num = document.getElementById('gameNum').value;
            if (num > random) {
                document.getElementById('remind').innerHTML = '啊欧~你猜大啦！！'
            } else if (num < random) {
                document.getElementById('remind').innerHTML = '哟呵~你猜小啦！！';
            } else {
                document.getElementById('remind').innerHTML = 'Wow!!你猜中了!!恭喜恭喜!!';
            }
        })
    })
    /******************************************************显示所有用户动态************************************************ */
    //页面布局
function toFix() {
    let left = document.querySelector('.left');
    let right = document.querySelector('.right');
    document.addEventListener('scroll', function() {
        if (window.pageYOffset >= 144) {
            left.style.position = 'fixed';
            right.style.position = 'fixed';
            left.style.left = '4.65rem';
            left.style.top = '4rem';
            right.style.right = '4.65rem';
            right.style.top = '4rem';
        } else {
            left.style.position = 'absolute';
            right.style.position = 'absolute';
            left.style.left = '0';
            left.style.top = '0.5rem';
            right.style.right = '0';
            right.style.top = '0.5rem';
        }
    })
}
toFix();

//格式化日期
function getTime(time) {
    var b = time.substr(0, 10);
    var c = time.substr(11, 8);
    return b + ' ' + c;
}
window.onload = function() {
        if (sessionStorage.getItem('visitor') == 1) {
            document.querySelector('.header').style.display = 'none';
            document.querySelector('.banner').style.display = 'none';
            document.querySelector('.left').style.display = 'none';
            document.querySelector('.right').style.display = 'none';
            let novisit = document.createElement('button');
            novisit.innerHTML = '去注册/退出';
            novisit.style.float = 'right';
            document.querySelector('.allFriH2').appendChild(novisit);
            novisit.addEventListener('click', function() {
                window.location.href = '../views/register.html';
            })
        }
        $.ajax({
            url: 'http://www.rushmc.top/api/dynamicState/select/all',
            type: 'get',
            datatype: 'json',
            success: function(res) {
                console.log(res);

                let ul = document.getElementById("ul");
                for (let i = 0; i < res.data.length; i++) {
                    let li = document.createElement('li');
                    ul.appendChild(li);
                    li.setAttribute("id", res.data[i]['id']);
                    //显示用户动态详情
                    li.addEventListener('click', function() {
                        localStorage.showUserId = res.data[i]['id'];
                        document.getElementById('show').style.display = 'block';
                        document.getElementById("center").style.display = "none";
                        $.ajax({
                            url: 'http://www.rushmc.top/api/dynamicState/search/' + res.data[i]['id'],
                            type: 'get',
                            data: { key: res.data[i]['id'] },
                            success: function(res) {
                                console.log(res);
                                let show = document.getElementById('show');
                                for (let i = 0; i < res.data.length; i++) {
                                    if (res.data[i].id == li.getAttribute('id')) {
                                        let h2 = document.createElement('h2');
                                        show.appendChild(h2);
                                        h2.innerHTML = res.data[i]['title'];
                                        let div1 = document.createElement('div');
                                        show.appendChild(div1);
                                        div1.setAttribute('class', 'div2')
                                        let span1 = document.createElement('span');
                                        div1.appendChild(span1);
                                        span1.innerHTML = 'id:' + res.data[i]['id'];
                                        let span2 = document.createElement('span');
                                        div1.appendChild(span2);
                                        span2.innerHTML = 'userId:' + res.data[i]['userId'];
                                        let span3 = document.createElement('span');
                                        div1.appendChild(span3);
                                        span3.innerHTML = 'username:' + res.data[i]['username'];
                                        let span4 = document.createElement('span');
                                        div1.appendChild(span4);
                                        span4.innerHTML = 'creationTime:' + getTime(res.data[i]['creationTime']);
                                        let div = document.createElement('div');
                                        show.appendChild(div);
                                        div.innerHTML = res.data[i]['content'];
                                        for (let j = 0; j < res.data[i]['urls'].length; j++) {
                                            let img = document.createElement('img');
                                            show.appendChild(img);
                                            img.src = 'http://www.rushmc.top/' + res.data[i]['urls'][j];
                                            img.style.marginRight = '0.5rem';
                                            img.addEventListener('click', function() {

                                                document.getElementById('showPic').style.display = 'block';
                                                document.getElementById('showPic').src = img.src;
                                            })
                                            document.getElementById('showPic').addEventListener('click', function() {

                                                document.getElementById('showPic').style.display = 'none';
                                                document.getElementById('showPic').src = '';
                                            })
                                        }
                                        let btnBox = document.createElement('div');
                                        document.getElementById("show").appendChild(btnBox);
                                        btnBox.setAttribute('class', 'btnBox');
                                        let btn = document.createElement('button');
                                        btnBox.appendChild(btn);
                                        btn.innerHTML = "返回";
                                        btn.addEventListener('click', function() {
                                                document.getElementById("center").style.display = "block";
                                                document.getElementById("show").style.display = "none";
                                                let pObj = show.childNodes;
                                                for (let i = pObj.length - 1; i >= 0; i--) {
                                                    show.removeChild(pObj[i]);
                                                }
                                            })
                                            //发表评论
                                        let comment = document.createElement('div');
                                        show.appendChild(comment);
                                        comment.setAttribute('class', 'comment');
                                        let comH2 = document.createElement('h2');
                                        comment.appendChild(comH2);
                                        comH2.innerHTML = '评论区';
                                        let addComment = document.createElement('input');
                                        addComment.type = 'text';
                                        addComment.setAttribute('class', 'addComment');
                                        comment.appendChild(addComment);
                                        let addComBtn = document.createElement('button');
                                        comment.appendChild(addComBtn);
                                        addComBtn.setAttribute('class', 'addComBtn');
                                        addComBtn.innerHTML = '发表评论';
                                        addComBtn.addEventListener('click', function() {
                                                let commentData = new FormData();
                                                commentData.append('dynamicStateId', res.data[i]['id']);
                                                commentData.append('replyerId', localStorage.getItem('id'));
                                                commentData.append('beReplyerId', res.data[i]['userId']);
                                                commentData.append('commentsContent', addComment.value.toString());
                                                $.ajax({
                                                    url: 'http://www.rushmc.top/api/comment/insert',
                                                    type: 'post',
                                                    data: commentData,
                                                    contentType: false,
                                                    //取消帮我们格式化数据，是什么就是什么
                                                    processData: false,
                                                    success: function(res) {
                                                        console.log(res);
                                                        let newcomment = document.createElement('div');
                                                        comment.appendChild(newcomment);
                                                        newcomment.innerHTML = localStorage.getItem('username') + ':' + addComment.value.toString();
                                                    },
                                                    error: function(res) {
                                                        console.log(res);

                                                        toTold.innerHTML = '发表评论失败';
                                                        toTold.style.color = 'red';
                                                        open();
                                                        setTimeout('close()', 3000);
                                                    }
                                                });
                                            })
                                            //根据动态id查询评论并显示
                                        $.ajax({
                                            url: 'http://www.rushmc.top/api/comment/allComment',
                                            type: 'get',
                                            data: {
                                                dynamicStateId: res.data[i]['id']
                                            },
                                            success: function(res) {
                                                console.log(res);
                                                for (let i = 0; i < res.data.length; i++) {
                                                    if (res.data[i].replyId == 0) {
                                                        let pp = res.data[i].id;
                                                        let newcomment = document.createElement('div');
                                                        comment.appendChild(newcomment);
                                                        newcomment.setAttribute('class', 'newComment');
                                                        newcomment.innerHTML = res.data[i].replyerId + ':' + res.data[i].commentsContent;
                                                        let delComment = document.createElement('button');
                                                        newcomment.appendChild(delComment);
                                                        delComment.setAttribute('class', 'delComment');
                                                        delComment.innerHTML = '删除评论';
                                                        delComment.addEventListener('click', function(e) {
                                                            e.stopPropagation();
                                                            $.ajax({
                                                                url: 'http://www.rushmc.top/api/comment/delete',
                                                                type: 'post',
                                                                data: { id: res.data[i].id },
                                                                success: function(res) {
                                                                    console.log(res);
                                                                    open();
                                                                    toTold.innerHTML = '删除成功';
                                                                    toTold.style.color = 'green';
                                                                    setTimeout('close()', 3000);
                                                                },
                                                                error: function(res) {
                                                                    console.log(res);
                                                                    open();
                                                                    toTold.innerHTML = '删除失败';
                                                                    toTold.style.color = 'red';
                                                                    setTimeout('close()', 3000);
                                                                }
                                                            })
                                                        })

                                                        //根据动态id查询回复并显示
                                                        $.ajax({
                                                                url: 'http://www.rushmc.top/api/getReplyById',
                                                                type: 'post',
                                                                data: { "dynamicId": res.data[i].dynamicStateId },
                                                                success: function(res) {
                                                                    console.log(res);
                                                                    for (let i = 0; i < res.data.length; i++) {
                                                                        if (res.data[i].replyId == pp) {
                                                                            let newReply = document.createElement('div');
                                                                            newcomment.appendChild(newReply);
                                                                            newReply.setAttribute('class', 'newReply');
                                                                            newReply.innerHTML = res.data[i].replyerName + ' 回复 ' + res.data[i].beReplyerName + ' ：' + res.data[i].commentsContent;
                                                                            let newReplyDel = document.createElement('button');
                                                                            newReply.appendChild(newReplyDel);
                                                                            newReplyDel.innerHTML = '删除回复';
                                                                            newReplyDel.setAttribute('class', 'newReplyDel');
                                                                            newReplyDel.addEventListener('click', function(e) {
                                                                                e.stopPropagation();
                                                                                $.ajax({
                                                                                    url: 'http://www.rushmc.top/api/comment/delete',
                                                                                    type: 'post',
                                                                                    data: { id: res.data[i].id },
                                                                                    success: function(res) {
                                                                                        console.log(res);
                                                                                        open();
                                                                                        toTold.innerHTML = '删除成功';
                                                                                        toTold.style.color = 'green';
                                                                                        setTimeout('close()', 3000);
                                                                                    },
                                                                                    error: function(res) {
                                                                                        console.log(res);
                                                                                        open();
                                                                                        toTold.innerHTML = '删除失败';
                                                                                        toTold.style.color = 'red';
                                                                                        setTimeout('close()', 3000);
                                                                                    }
                                                                                })
                                                                            })
                                                                        }
                                                                    }
                                                                },
                                                                error: function(res) {
                                                                    console.log(res);
                                                                }
                                                            })
                                                            //添加回复模块
                                                        newcomment.addEventListener('click', function() {
                                                            let replyBox = document.createElement('span');
                                                            comment.appendChild(replyBox);
                                                            comment.class = 'comment';
                                                            replyBox.class = 'replyBox';
                                                            let reply = document.createElement('input');
                                                            replyBox.appendChild(reply);
                                                            reply.type = 'text';
                                                            let replyBtn = document.createElement('button');
                                                            replyBox.appendChild(replyBtn);
                                                            replyBtn.innerHTML = '回复';
                                                            let replyBtnNo = document.createElement('button');
                                                            replyBox.appendChild(replyBtnNo);
                                                            replyBtnNo.innerHTML = '取消';
                                                            replyBtn.addEventListener('click', function() {
                                                                let replyData = new FormData();
                                                                replyData.append('dynamicStateId', res.data[i].dynamicStateId);
                                                                replyData.append('replyId', res.data[i].id);
                                                                replyData.append('replyerId', localStorage.getItem('id'));
                                                                replyData.append('beReplyerId', res.data[i].beReplyerId);
                                                                replyData.append('commentsContent', reply.value);
                                                                $.ajax({
                                                                    url: 'http://www.rushmc.top/api/comment/reply',
                                                                    type: 'post',
                                                                    data: replyData,
                                                                    contentType: false,
                                                                    processData: false,
                                                                    success: function(res) {
                                                                        console.log(res);
                                                                        replyBox.style.display = 'none';
                                                                        open();
                                                                        toTold.innerHTML = '回复成功';
                                                                        toTold.style.color = 'green';
                                                                        setTimeout('close()', 3000);
                                                                    },
                                                                    error: function(res) {
                                                                        console.log(res);
                                                                        open();
                                                                        toTold.innerHTML = '回复失败';
                                                                        toTold.style.color = 'red';
                                                                        setTimeout('close()', 3000);
                                                                    }
                                                                })
                                                            })
                                                            replyBtnNo.addEventListener('click', function() {
                                                                replyBox.style.display = 'none';
                                                            })
                                                        })
                                                    }
                                                }
                                            },
                                            error: function(res) {
                                                console.log(res);
                                                open();
                                                toTold.innerHTML = 'error';
                                                toTold.style.color = 'red';
                                                setTimeout('close()', 3000);
                                            }
                                        })
                                    }
                                }
                            },
                            error: function(res) {
                                console.log(res);
                                open();
                                toTold.innerHTML = 'error';
                                toTold.style.color = 'red';
                                setTimeout('close()', 3000);
                            }
                        });
                    });
                    let h2 = document.createElement('h2');
                    li.appendChild(h2);
                    h2.innerHTML = res.data[i]['title'];
                    let div1 = document.createElement('div');
                    li.appendChild(div1);
                    div1.setAttribute('class', 'div1');
                    let span1 = document.createElement('span');
                    div1.appendChild(span1);
                    span1.innerHTML = 'id:' + res.data[i]['id'];
                    let span2 = document.createElement('span');
                    div1.appendChild(span2);
                    span2.innerHTML = 'userId:' + res.data[i]['userId'];
                    let span3 = document.createElement('span');
                    div1.appendChild(span3);
                    span3.innerHTML = 'username:' + res.data[i]['username'];
                    let span4 = document.createElement('span');
                    div1.appendChild(span4);
                    span4.innerHTML = 'creationTime:' + getTime(res.data[i]['creationTime']);
                    let div = document.createElement('div');
                    li.appendChild(div);
                    div.innerHTML = res.data[i]['content'];
                }
            },
            error: function(res) {
                console.log(res);
                let li = document.createElement('li');
                ul.appendChild(li);
                li.innerHTML = "暂无动态";
            }
        });
    }
    /*******************************************************删除动态************************************************** */
document.getElementById('dell').onclick = function() {
        document.getElementById('showPic').style.display = 'none';
        document.getElementById('showPic').src = '';
        let del = document.getElementById("del");
        $.ajax({
            url: 'http://www.rushmc.top/api/dynamicState/delete',
            type: 'post',
            data: { id: del.value },
            success: function(res) {
                console.log(res);
                open();
                toTold.innerHTML = res.msg;
                toTold.style.color = 'green';
                setTimeout('close()', 3000);
            },
            error: function(res) {
                console.log(res);
                open();
                toTold.innerHTML = res.msg;
                toTold.style.color = 'red';
                setTimeout('close()', 3000);
            }
        });
    }
    //**********************************************************搜索动态*********************************************************** */
document.getElementById('findBtn').addEventListener('click', function() {
        document.getElementById('showPic').style.display = 'none';
        document.getElementById('showPic').src = '';
        document.getElementById('show').style.display = 'none';
        document.getElementById('findDy').style.display = 'block';
        document.getElementById('center').style.display = 'block';
        let findId = document.getElementById('find').value;
        let pObj = document.getElementById("findUl").childNodes;
        for (let i = pObj.length - 1; i >= 0; i--) {
            document.getElementById("findUl").removeChild(pObj[i]);
        }
        if (findId != null) {
            $.ajax({
                url: 'http://www.rushmc.top/api/dynamicState/select/' + findId,
                type: 'get',
                data: { key: findId },
                success: function(res) {
                    console.log(res);
                    document.getElementById('reveal').style.display = 'none';
                    document.getElementById('findDy').style.display = 'block';
                    let ul = document.getElementById('findUl');
                    let btn = document.createElement('button');
                    ul.appendChild(btn);
                    btn.setAttribute('class', 'toReveal');
                    btn.innerHTML = "返回";
                    btn.addEventListener('click', function() {
                        document.getElementById("reveal").style.display = "block";
                        document.getElementById("findDy").style.display = "none";
                        let pObj = document.getElementById("findUl").childNodes;
                        for (let i = pObj.length - 1; i >= 0; i--) {
                            document.getElementById("findUl").removeChild(pObj[i]);
                        }
                    })
                    for (let i = 0; i < res.data.length; i++) {
                        let li = document.createElement('li');
                        li.setAttribute("id", res.data[i]['id']);
                        li.setAttribute("class", res.data[i]['userId']);
                        ul.appendChild(li);
                        li.addEventListener('click', function() {
                            document.getElementById('center').style.display = 'none';
                            document.getElementById('findShow').style.display = 'block';
                            let show = document.getElementById('findShow');
                            $.ajax({
                                url: 'http://www.rushmc.top/api/dynamicState/search/' + li.getAttribute('id'),
                                type: 'get',
                                data: { key: li.getAttribute('id') },
                                success: function(res) {
                                    console.log(res);
                                    for (let i = 0; i < res.data.length; i++) {
                                        if (res.data[i].id == li.getAttribute('id')) {
                                            let h2 = document.createElement('h2');
                                            show.appendChild(h2);
                                            h2.innerHTML = res.data[i]['title'];
                                            let div1 = document.createElement('div');
                                            show.appendChild(div1);
                                            div1.setAttribute('class', 'div4')
                                            let span1 = document.createElement('span');
                                            div1.appendChild(span1);
                                            span1.innerHTML = 'id:' + res.data[i]['id'];
                                            let span2 = document.createElement('span');
                                            div1.appendChild(span2);
                                            span2.innerHTML = 'userId:' + res.data[i]['userId'];
                                            let span3 = document.createElement('span');
                                            div1.appendChild(span3);
                                            span3.innerHTML = 'username:' + res.data[i]['username'];
                                            let span4 = document.createElement('span');
                                            div1.appendChild(span4);
                                            span4.innerHTML = 'creationTime:' + getTime(res.data[i]['creationTime']);
                                            let div = document.createElement('div');
                                            show.appendChild(div);
                                            div.innerHTML = res.data[i]['content'];
                                            for (let j = 0; j < res.data[i]['urls'].length; j++) {
                                                let img = document.createElement('img');
                                                show.appendChild(img);
                                                img.src = 'http://www.rushmc.top/' + res.data[i]['urls'][j];
                                            }
                                            let btnBox = document.createElement('div');
                                            document.getElementById("findShow").appendChild(btnBox);
                                            btnBox.setAttribute('class', 'findBtnBox');
                                            let btn = document.createElement('button');
                                            btnBox.appendChild(btn);
                                            btn.innerHTML = "返回";
                                            btn.addEventListener('click', function() {
                                                document.getElementById("center").style.display = "block";
                                                document.getElementById("findShow").style.display = "none";
                                                let pObj = show.childNodes;
                                                for (let i = pObj.length - 1; i >= 0; i--) {
                                                    show.removeChild(pObj[i]);
                                                }
                                            })
                                        }
                                    }
                                },
                                error: function(res) {
                                    console.log(res);
                                    open();
                                    toTold.innerHTML = 'error';
                                    toTold.style.color = 'red';
                                    setTimeout('close()', 3000);
                                }
                            })
                        })
                        let h2 = document.createElement('h2');
                        li.appendChild(h2);
                        h2.innerHTML = res.data[i]['title'];
                        let div3 = document.createElement('div');
                        li.appendChild(div3);
                        div3.setAttribute('class', 'div3');
                        let span1 = document.createElement('span');
                        div3.appendChild(span1);
                        span1.innerHTML = 'id:' + res.data[i]['id'];
                        let span2 = document.createElement('span');
                        div3.appendChild(span2);
                        span2.innerHTML = 'userId:' + res.data[i]['userId'];
                        let span3 = document.createElement('span');
                        div3.appendChild(span3);
                        span3.innerHTML = 'username:' + res.data[i]['username'];
                        let span4 = document.createElement('span');
                        div3.appendChild(span4);
                        span4.innerHTML = 'creationTime:' + getTime(res.data[i]['creationTime']);
                        let div = document.createElement('div');
                        li.appendChild(div);
                        div.innerHTML = res.data[i]['content'];

                    }
                },
                error: function(res) {
                    console.log(res);
                    open();
                    toTold.innerHTML = res.msg;
                    toTold.style.color = 'red';
                    setTimeout('close()', 3000);
                }
            })
        } else {
            open();
            toTold.innerHTML = '查询id不能为空';
            toTold.style.color = 'red';
            setTimeout('close()', 3000);
        }

    })
    // *********************************主题切换************************************************
document.getElementById('blue').addEventListener('click', function() {
    document.documentElement.style.backgroundColor = 'rgb(195, 224, 233)';
    document.querySelector('.left').style.backgroundColor = 'rgba(151, 182, 216, 0.7)';
    document.querySelector('.right').style.backgroundColor = '#afc8e0';
    document.getElementById('reveal').style.backgroundColor = 'rgba(160, 180, 220, 0.5)';
})
document.getElementById('white').addEventListener('click', function() {
    document.documentElement.style.backgroundColor = '#fbf7f7';
    document.querySelector('.left').style.backgroundColor = 'white';
    document.querySelector('.right').style.backgroundColor = 'white';
    document.getElementById('reveal').style.background = 'rgb(155 159 167 / 50%)';
})